package com.canbot.u05.update;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.util.Log;

import java.io.File;

public class RobotUtil {

        private static long starttime;

        /**
         * 方法描述：获取本地userId方法
         *
         * @param String app_name
         * @return
         * @see RobotUtil
         */
        public static String getUserId(Context context) {
                WifiManager wifi = (WifiManager) context
                        .getSystemService(Context.WIFI_SERVICE);
                if (!wifi.isWifiEnabled()) {
                        wifi.setWifiEnabled(true);
                }
                WifiInfo info = wifi.getConnectionInfo();
                String mac = info.getMacAddress();
                if (mac == null) {
                        if (!wifi.isWifiEnabled()) {
                                wifi.setWifiEnabled(true);
                        }
                        starttime = System.currentTimeMillis();
                        while (true && System.currentTimeMillis() - starttime < 5000) {
                                try {
                                        Thread.sleep(1000);
                                }
                                catch (InterruptedException e) {
                                        e.printStackTrace();
                                }
                                info = wifi.getConnectionInfo();
                                mac = info.getMacAddress();
                                if (mac != null) {
                                        break;

                                }
                        }
                        if (mac == null) {
                                Log.e("getUserId", "getUserId失败 ");
                                return null;
                        }

                }
                Log.e("getUserId", "getUserId " + mac);
                return mac.replace(":", "");
                // StringBuffer a = new StringBuffer(mac);
                // a.deleteCharAt(2).deleteCharAt(4).deleteCharAt(6).deleteCharAt(8)
                // .deleteCharAt(10);
                // mac = a.toString();
                // return "ainuo" + str;
        }

        /**
         * 检测wifi是否可用
         *
         * @param context
         * @return
         */
        public static boolean IsStartWifi(final Context context) {
                try {
                        WifiManager wm = (WifiManager) context
                                .getSystemService(Context.WIFI_SERVICE);
                        return (wm != null
                                && WifiManager.WIFI_STATE_ENABLED == wm.getWifiState());
                }
                catch (Exception e) {
                }
                return false;
        }

        /**
         * 检测网络是否可用
         *
         * @return
         */
        public static boolean isNetworkConnected(Context context) {
                ConnectivityManager cm = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo ni = cm.getActiveNetworkInfo();
                return ni != null && ni.isConnectedOrConnecting();
        }

        public static void install(File file, Context context) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setAction(android.content.Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file),
                        "application/vnd.android.package-archive");
                context.startActivity(intent);
        }

        // 判断apk是否已安装
        public static boolean isPkgInstalled(Context ctx, String packageName,
                                             String tag) {
                PackageManager pm = ctx.getPackageManager();
                Log.e(tag, "isPkgInstalledp00-000" + packageName);
                try {
                        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
                }
                catch (NameNotFoundException e) {
                        e.printStackTrace();
                        return false;
                }
                return true;
        }

        public static String getDeviceVersion() {
                String device_version = "";
                String bversion = Build.VERSION.INCREMENTAL;
                Log.e("check", bversion);
                if (bversion != null && bversion.contains(".")) {
                        String[] arr = bversion.split("\\.");
                        if (arr != null && arr.length >= 3) {
                                device_version = arr[2].trim();
                        }
                }
                return device_version;
        }

}